package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.exception.NotFoundException
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil
import photobox.manage.MemberService
import photobox.account.User

class MngMemberController extends BaseController{
    def memberService
    def indexCustomer() {}
    def indexPhotographer() {}
    def listCustomer(){
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "username", "nickName", "email","presentation"], [sort: 'id', order: "desc"])
        def userRs = memberService.selectCustomer(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(userRs.rs, userRs.total, pageParams,
                [id: 'id', username: 'username', nickName: 'nickName', enabled: 'enabled', realName: 'realName',presentation:'presentation']) as JSON
    }
    def listPhotographer(){
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "username", "nickName", "email","presentation"], [sort: 'id', order: "desc"])
        def userRs = memberService.selectPhotographer(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(userRs.rs, userRs.total, pageParams,
                [id: 'id', username: 'username', nickName: 'nickName', enabled: 'enabled', realName: 'realName',presentation:'presentation']) as JSON

    }
    def createCustomer(){}
    def saveCustomer(){
        memberService.saveCustomer(params)
        render([error: 0, message: "创建新客户完成"] as JSON)
    }
    def createPhotographer(){}
    def savePhotographer(){
        memberService.saveCustomer(params)
        render([error: 0, message: "创建新客户完成"] as JSON)
    }
    def edit() {
        def user = User.get(params.id as Long)

        if (!user) {
            throw new NotFoundException("没有找到对应的用户")
        }
        return [user: user]
    }

    def update() {
        memberService.updateCustomer(params)
        render([error: 0, message: "更新用户信息完成"] as JSON)
    }

    def delete() {
        memberService.deleteCustomer(params)
        render([error:0, message: "删除用户完成"] as JSON)
    }
}
